
COMPILE_OPT IDL2, LOGICAL_PREDICATE
conf = omni_load_conf()
s    = omni_read_cat(conf.cat,ncat)
restore,'./local/'+conf.survey+'_velocities.sav',/ver


gfl = (v.grs.flag EQ 1) OR (v.grs.flag EQ 2) OR $
      (v.grs.flag EQ 5) OR (v.grs.flag EQ 6)

ind = where(gfl OR v.vlsr GE -500, nvel)   
print,nvel
v = v[ind]
s = s[ind]

;;===================================================
;; Open the file, write the table header

openw,lun,'./grs_paper/kinematic_table.tex',/get_lun


printf,lun,'%%=================================%%'
printf,lun,'%%                                 %%'
printf,lun,'%%   Table: Complete V2 Velocities %%'
printf,lun,'%%   Label: table:velocities       %%'
printf,lun,'%%                                 %%'
printf,lun,'%%=================================%%'
printf,lun,'\begin{deluxetable}{ccccc}'
printf,lun,'  \tablecolumns{5}'
printf,lun,'  \tablewidth{0pt}'
printf,lun,'  \tabletypesize{\footnotesize}'
printf,lun,'  \tablecaption{Complete V2 Kinematic Information\label{table:velocities}}'
printf,lun,'  \tablehead{'
printf,lun,'    \colhead{Catalog}  & \colhead{$\ell$} & \colhead{$b$} & \colhead{\vlsr} & \colhead{Ref.} \\'
printf,lun,'    \colhead{Number} & \colhead{(\degr)} & \colhead{(\degr)} & \colhead{(\kms)} & \colhead{}'
printf,lun,'  }'
printf,lun,'  \startdata'


FOR ii=2358,2367 DO BEGIN
   
   str = '  '+string(v[ii].cnum,v[ii].l,v[ii].b,$
                     format="(I0,' & ',F0.3,' & ',F+0.3,' & ')")
   IF v[ii].vlsr GE -500 THEN BEGIN
      str += string(v[ii].vlsr,format="(F0.1)")+' & '
      
      first = 1b
      FOR jj=0,6 DO BEGIN
         thisflag = v[ii].rv_types AND 2^jj
         IF thisflag THEN BEGIN
            IF ~first THEN str+=','
            CASE thisflag OF
               1: str += '1'
               2: str += '1'
               4: str += '2'
               8: str += (v[ii].l LT 120) ? '3' : '4' 
            ENDCASE
            first = 0b
         ENDIF
      ENDFOR
      str += ' \\'
   ENDIF ELSE str += string(v[ii].grs.vlsr,format="(F0.1)")+' & 5 \\'
   
   
   printf,lun,str

ENDFOR
;; printf,lun,'  & & \nodata & & '


printf,lun,'  \enddata'
printf,lun,'  \tablerefs{1: \citet{Shirley:2013}; 2. \citet{Shirley:2013b}, 3. \citet{Dunham:2011c}, 4. \citet{Dunham:2010}, 5. GRS (this work).}'
printf,lun,'  \tablecomments{This table is available in its entirety in a machine-readable format in the online journal.  A portion is shown here for guidance regarding its form and content.}'
printf,lun,'\end{deluxetable}'

close,lun
free_lun,lun


nhcop = 0
nn2hp = 0
nnh3  = 0
ncs   = 0
ngrs  = 0
nwtf  = 0
nc18o = 0

FOR ii=0,nvel-1 DO BEGIN
   
   IF v[ii].vlsr GE -500 THEN BEGIN
      FOR jj=0,6 DO BEGIN       ; Loop over RV types
         thisflag = v[ii].rv_types AND 2^jj
         CASE thisflag OF
            0:
            1: nhcop++
            2: nn2hp++
            4: ncs++
            8: nnh3++
            16: nc18o++
            32: nwtf++
            64: nwtf++
            128: nwtf++
            256: nwtf++
         ENDCASE
      ENDFOR
   ENDIF 
   
   
   
ENDFOR
ngrs = fix(total(gfl))

openw,lun,'./grs_paper/kinematic_distro.tex',/get_lun


printf,lun,'%%=================================%%'
printf,lun,'%%                                 %%'
printf,lun,'%%   Table: N Sources per Tracer   %%'
printf,lun,'%%   Label: table:ntracer          %%'
printf,lun,'%%                                 %%'
printf,lun,'%%=================================%%'
printf,lun,'\begin{deluxetable}{lcc}'
printf,lun,'  \tablecolumns{3}'
printf,lun,'  \tablewidth{0pt}'
printf,lun,'  \tabletypesize{\footnotesize}'
printf,lun,'  \tablecaption{Number of BGPS V2 Sources for Each Velocity Tracer\label{table:ntracer}}'
printf,lun,'  \tablehead{'
printf,lun,'    \colhead{Species}  & \colhead{$N$} & \colhead{Ref.}'
printf,lun,'  }'
printf,lun,'  \startdata'
printf,lun,'  \hcop(3-2) & '+string(nhcop,format="(I0)")+' & 1 \\'
printf,lun,'  \nnhp(3-2) & '+string(nn2hp,format="(I0)")+' & 1 \\'
printf,lun,'  CS(2-1) & '+string(ncs,format="(I0)")+' & 2 \\'
printf,lun,'  \nhhh(1,1) & '+string(nnh3,format="(I0)")+' & 3,4 \\'
printf,lun,'  C$^{18}$O(2-1) & '+string(nc18o,format="(I0)")+' & 5 \\'
printf,lun,'  \thco(1-0) & '+string(ngrs,format="(I0)")+' & 6'


print,nhcop,nn2hp,ncs,nnh3,ngrs

printf,lun,'  \enddata'
printf,lun,'  \tablerefs{1: \citet{Shirley:2013}; 2. Y. Shirley (2012, private communication), 3. \citet{Dunham:2011c}, 4. \citet{Dunham:2010}, 5. M. Lichtenberger (2014, private communication), 6. GRS (this work).}'
printf,lun,'\end{deluxetable}'
close,lun
free_lun,lun


END
